package com.ruoyi.html.handle.service.impl;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.game.api.RemoteGameItemService;
import com.ruoyi.game.api.domin.GameItem;
import com.ruoyi.html.handle.annotation.SelectType;
import com.ruoyi.html.handle.domin.GameItemHandleIns;
import com.ruoyi.html.handle.exception.GameItemHandleInsException;
import com.ruoyi.html.handle.service.IGameItemHandleInsService;
import com.ruoyi.html.handle.service.ISelectHandleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 游戏物品解析添加
 *
 * @author : 小胖
 * @version : 1.0
 * @date : 2022/05/08  17:03
 **/
@Service
@RequiredArgsConstructor
public class GameItemHandleInsServiceImpl implements ApplicationContextAware, IGameItemHandleInsService
{
    private final RemoteGameItemService remoteGameItemService;

    private Map<String, ISelectHandleService> selectHandleServiceMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        selectHandleServiceMap = applicationContext.getBeansOfType(ISelectHandleService.class)
                .values()
                .stream()
                .filter(service -> AnnotationUtil.hasAnnotation(service.getClass(), SelectType.class))
                .collect(Collectors.toMap(service -> AnnotationUtil.getAnnotationValue(service.getClass(), SelectType.class), Function.identity()));
    }

    /**
     * 解析添加
     *
     * @param gameItemHandleIns 解析添加
     */
    @Override
    public void handleIns(GameItemHandleIns gameItemHandleIns)
    {
        // 获取对应解析实现, 并解析获取结果
        List<GameItem> gameItems = Assert.notNull(selectHandleServiceMap.get(gameItemHandleIns.getHandleType()), () -> new GameItemHandleInsException("无对应解析实现"))
                .selectHandle(gameItemHandleIns.getHtml());
        if (CollUtil.isEmpty(gameItems))
        {
            throw new GameItemHandleInsException("解析可能出错了, 未解析到任何结果");
        }
        gameItemHandleIns.setCreateBy(SecurityUtils.getUsername());
        gameItemHandleIns.setCreateTime(DateUtil.date());
        // 完善对象属性
        gameItems.forEach(gameItem -> BeanUtil.copyProperties(gameItemHandleIns, gameItem));
        // 添加到数据库
        R<Boolean> result = remoteGameItemService.add(gameItems, SecurityConstants.INNER);
        // 判断是否请求成功
        Assert.isFalse(result.getCode() != R.SUCCESS, () -> new ServiceException(result.getMsg(), result.getCode()));
    }
}
